package com.lxmes.hr.service.impl;

import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcGsjs;
import com.lxmes.hr.domain.NcWagesGzhs;
import com.lxmes.hr.mapper.NcWagesGzhsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcWagesPaymentMapper;
import com.lxmes.hr.domain.NcWagesPayment;
import com.lxmes.hr.service.INcWagesPaymentService;

import javax.validation.Validator;

/**
 * 工资发放总Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-25
 */
@Service
public class NcWagesPaymentServiceImpl implements INcWagesPaymentService
{
    @Autowired
    private NcWagesPaymentMapper ncWagesPaymentMapper;

    @Autowired
    private NcWagesGzhsMapper ncWagesGzhsMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询工资发放总
     *
     * @param id 工资发放总主键
     * @return 工资发放总
     */
    @Override
    public NcWagesPayment selectNcWagesPaymentById(Long id)
    {
        return ncWagesPaymentMapper.selectNcWagesPaymentById(id);
    }

    /**
     * 查询工资发放总列表
     *
     * @param ncWagesPayment 工资发放总
     * @return 工资发放总
     */
    @Override
    public List<NcWagesPayment> selectNcWagesPaymentList(NcWagesPayment ncWagesPayment)
    {
        return ncWagesPaymentMapper.selectNcWagesPaymentList(ncWagesPayment);
    }

    /**
     * 新增工资发放总
     *
     * @param ncWagesPayment 工资发放总
     * @return 结果
     */
    @Override
    public int insertNcWagesPayment(NcWagesPayment ncWagesPayment)
    {
        return ncWagesPaymentMapper.insertNcWagesPayment(ncWagesPayment);
    }

    /**
     * 修改工资发放总
     *
     * @param ncWagesPayment 工资发放总
     * @return 结果
     */
    @Override
    public int updateNcWagesPayment(NcWagesPayment ncWagesPayment)
    {
        return ncWagesPaymentMapper.updateNcWagesPayment(ncWagesPayment);
    }

    /**
     * 批量删除工资发放总
     *
     * @param ids 需要删除的工资发放总主键
     * @return 结果
     */
    @Override
    public int deleteNcWagesPaymentByIds(Long[] ids)
    {
        return ncWagesPaymentMapper.deleteNcWagesPaymentByIds(ids);
    }

    /**
     * 删除工资发放总信息
     *
     * @param id 工资发放总主键
     * @return 结果
     */
    @Override
    public int deleteNcWagesPaymentById(Long id)
    {
        return ncWagesPaymentMapper.deleteNcWagesPaymentById(id);
    }

    @Override
    public String imporIteminfo(List<NcWagesPayment> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcWagesPayment org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcWagesPayment(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工姓名 " + org.getWaName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getWaName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int tbGzhs() {
        List<NcWagesGzhs> ncWagesGzhs = ncWagesGzhsMapper.selectNcWagesGzhsList(null);
        for(NcWagesGzhs gzhs : ncWagesGzhs){
            NcWagesPayment ncWagesPayment = new NcWagesPayment();
            BeanUtils.copyProperties(gzhs,ncWagesPayment);
            insertNcWagesPayment(ncWagesPayment);
        }
        return 1;
    }
}
